package com.test.thread.consumer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Client {

	public static void main(String[] args) {
		Q q = new Q();

		new Consumer(q);
		new Producer(q);
		try {
			Context ctx = new InitialContext(); // 得到初始化上下文
			Object obj = ctx.lookup("java:/MySqlDS");// 查找连接池
			DataSource ds = (DataSource) obj;// 转换成DataSource
			Connection connect = ds.getConnection();// 从连接池中得到一个连接
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery("select * from user");// 这是你的数据库的表噢!
			while (rs.next()) {
				System.out.println(rs.getString(1));// 取出1个字段
			}
			rs.close();
			stmt.close();
			connect.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

	}

}
